﻿"use strict";

var kospa = kospa || {};
kospa.ViewModel = kospa.ViewModel || {};

kospa.ViewModel = function (dataModel) {
	var self = this;
	self.Views = {
		Loading: {}
	};
	self.view = ko.observable(self.Views.Loading);
	self.loading = ko.computed(function () {
		return self.view() === self.Views.Loading;
	});

	self.addViewModel = function (options) {
		var viewItem = {};
		var navigator;
		self.Views[options.name] = viewItem;
		self[options.bindingMemberName] = ko.computed(function () {
			if (self.view() !== viewItem) {
				return null;
			}
			return new options.factory(self, dataModel);
		});

		if (typeof (options.navigatorFactory) !== "undefined") {
			navigator = options.navigatorFactory(self, dataModel);
		} else {
			navigator = function () {
				self.view(viewItem);
			};
		}
		self["navigateTo" + options.name] = navigator;
	};

	self.initialize = function (ref) {
		self.initGrid(dataModel.gridSettings[ref]);
		self.navigateToSearch(ref);
	}
}
